using LiJ.Common.Core;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Statistic.Mos;
using System;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Statistic
{
	/// <summary>
	/// 接口统计接口
	/// </summary>
	/// <summary>
	/// 微信统计接口
	/// </summary>
	/// <summary>
	/// 微信用户统计接口
	/// </summary>
	public class WxOffStatApi : WxOffBaseApi
	{
		/// <summary>
		///  获取指定某天 【群发】文章组合 在当天的统计数据
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatSendMo>> GetSendAticleStatisticAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getarticlesummary";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				end_date = date,
				begin_date = date
			});
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatSendMo>>(LHttpRequest, null);
		}

		/// <summary>
		///  获取指定某天【群发】文章组合连续最多七天的统计数据
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatSendTotalMo>> GetSendAticleStatisticTotalAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getarticletotal";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				end_date = date,
				begin_date = date
			});
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatSendTotalMo>>(LHttpRequest, null);
		}

		/// <summary>
		///  获取指定某段时间 微信文章 的统计数据
		/// </summary>
		/// <param name="statisticReq">时间跨度最大【三天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatDaliyMo>> GetAticleUserReadStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getuserread";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatDaliyMo>>(LHttpRequest, null);
		}

		/// <summary>
		///  获取指定某天 微信文章 的分时统计数据
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatHourMo>> GetAticleUserReadHourStatisticAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getuserreadhour";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				end_date = date,
				begin_date = date
			});
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatHourMo>>(LHttpRequest, null);
		}

		/// <summary>
		///  获取指定某段时间 微信文章 的分享统计数据
		/// </summary>
		/// <param name="statisticReq">时间跨度最大【七天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatShareMo>> GetAticleUserShareStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getusershare";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatShareMo>>(LHttpRequest, null);
		}

		/// <summary>
		///  获取指定某天 微信文章 的分享分时统计数据
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public async Task<WxStatResp<WxArticleStatShareHourMo>> GetAticleShareHourStatisticAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getusersharehour";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				end_date = date,
				begin_date = date
			});
			return await base.RestCommonOffcialAsync<WxStatResp<WxArticleStatShareHourMo>>(LHttpRequest, null);
		}

		/// <summary>
		///   接口调用分析接口
		///     最大时间宽度【三十天】
		/// </summary>
		/// <param name="statReq"> 最大时间宽度【三十天】</param>
		/// <returns></returns>
		public async Task<WxInterfaceStatResp> GetInterfaceStatisticAsync(WxStatReq statReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getinterfacesummary";
			LHttpRequest.CustomBody = this.GetRequestBody(statReq);
			return await base.RestCommonOffcialAsync<WxInterfaceStatResp>(LHttpRequest, null);
		}

		/// <summary>
		///   接口调用分析接口
		/// </summary>
		/// <param name="date"> </param>
		/// <returns></returns>
		public async Task<WxInterfaceStatResp> GetInterfaceHourStatisticAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getinterfacesummaryhour";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				begin_date = date,
				end_date = date
			});
			return await base.RestCommonOffcialAsync<WxInterfaceStatResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取消息统计概览
		///  时间宽度最多 【七天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【七天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatMo>> GetUpMsgStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsg";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取【分时】消息统计概览
		/// </summary>
		/// <param name="date"></param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatHourMo>> GetUpMsgHourStatisticAsync(DateTime date)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsghour";
			LHttpRequest.CustomBody = this.GetRequestBody(new WxStatReq
			{
				begin_date = date,
				end_date = date
			});
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatHourMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取【周】消息统计概览
		///  时间宽度最多 【三十天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【三十天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatMo>> GetUpMsgWeekStatistic(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsgweek";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取【月】消息统计概览
		///  时间宽度最多 【三十天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【三十天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatMo>> GetUpMsgMonthStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsgmonth";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取消息【分布】统计概览
		///  时间宽度最多 【十五天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【十五天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatDistMo>> GetUpMsgDistStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsgdist";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatDistMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取【周】消息【分布】统计概览
		///  时间宽度最多 【三十天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【三十天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatDistMo>> GetUpMsgDistWeekStatistic(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsgdistweek";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatDistMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取【月】消息【分布】统计概览
		///  时间宽度最多 【三十天】
		/// </summary>
		/// <param name="statisticReq">时间跨度最多【三十天】</param>
		/// <returns></returns>
		public async Task<WxStatResp<WxMsgUpStatDistMo>> GetUpMsgDistMonthStatisticAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getupstreammsgdistmonth";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxStatResp<WxMsgUpStatDistMo>>(LHttpRequest, null);
		}

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffStatApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		/// 获取用户增减数据
		/// 最大时间跨度   7
		/// </summary>
		/// <param name="statisticReq"></param>
		/// <returns></returns>
		public async Task<WxUserStatResp> GetUserSummaryAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getusersummary";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxUserStatResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取累计用户数据
		/// 最大时间跨度   7
		/// </summary>
		/// <param name="statisticReq"></param>
		/// <returns></returns>
		public async Task<WxUserStatResp> GetUserCumulateAsync(WxStatReq statisticReq)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/datacube/getusercumulate";
			LHttpRequest.CustomBody = this.GetRequestBody(statisticReq);
			return await base.RestCommonOffcialAsync<WxUserStatResp>(LHttpRequest, null);
		}

		/// <summary>
		///   返回请求参数json串
		/// </summary>
		/// <param name="statisticReq"></param>
		/// <returns></returns>
		private string GetRequestBody(WxStatReq statisticReq)
		{
			DateTime dateTime = statisticReq.begin_date;
			string arg = dateTime.ToString("yyyy-MM-dd");
			dateTime = statisticReq.end_date;
			return string.Format("{{\"begin_date\": \"{0}\", \"end_date\": \"{1}\"}}", arg, dateTime.ToString("yyyy-MM-dd"));
		}
	}
}
